package com.houweishi.module.realReport.service.impl;

import com.houweishi.common.constant.Constants;
import com.houweishi.common.core.domain.entity.SysDept;
import com.houweishi.common.core.domain.model.LoginUser;
import com.houweishi.common.exception.base.BaseException;
import com.houweishi.common.utils.DateUtils;
import com.houweishi.common.utils.SecurityUtils;
import com.houweishi.module.realReport.domain.RealReport;
import com.houweishi.module.realReport.mapper.RealReportMapper;
import com.houweishi.module.realReport.service.IRealReportService;
import com.houweishi.system.service.ISysDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 实时汇报Service业务层处理
 * 
 * @author yqq
 * @date 2023-06-28
 */
@Service
public class RealReportServiceImpl implements IRealReportService 
{
    @Resource
    private RealReportMapper realReportMapper;

    /**
     * 部门Service
     */
    @Resource
    private ISysDeptService iSysDeptService;

    /**
     * 查询实时汇报
     * 
     * @param id 实时汇报主键
     * @return 实时汇报
     */
    @Override
    public RealReport selectRealReportById(Long id)
    {
        return realReportMapper.selectRealReportById(id);
    }

    /**
     * 查询实时汇报列表
     * 
     * @param realReport 实时汇报
     * @return 实时汇报
     */
    @Override
    public List<RealReport> selectRealReportList(RealReport realReport)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.isNull(loginUser)){
            return null;
        }
        // 获取登录人部门
        SysDept dept = iSysDeptService.selectDeptById(loginUser.getDeptId());
        if (Objects.isNull(dept)) {
            throw new BaseException("部门信息为空");
        }
        // 获取当前人公司
        List<String> companyList = Arrays.asList(Constants.DX, Constants.YS, Constants.WY);
        if (companyList.contains(dept.getDeptName())) {
            realReport.setCompany(dept.getDeptName());
        } else if (Constants.HWS.equals(dept.getDeptName()) || Constants.SR.equals(dept.getDeptName())) {

        } else {
            realReport.setUserId(loginUser.getUserId());
        }
        // 子公司看子公司的，总公司看所有数据
        return realReportMapper.selectRealReportList(realReport);
    }

    /**
     * 新增实时汇报
     * 
     * @param realReport 实时汇报
     * @return 结果
     */
    @Override
    public int insertRealReport(RealReport realReport)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.isNull(loginUser)){
            return 0;
        }
        realReport.setUserId(loginUser.getUserId());
        realReport.setUserName(loginUser.getUsername());
        realReport.setCreateTime(DateUtils.getNowDate());
        return realReportMapper.insertRealReport(realReport);
    }

    /**
     * 修改实时汇报
     * 
     * @param realReport 实时汇报
     * @return 结果
     */
    @Override
    public int updateRealReport(RealReport realReport)
    {
        realReport.setUpdateTime(DateUtils.getNowDate());
        return realReportMapper.updateRealReport(realReport);
    }

    /**
     * 批量删除实时汇报
     * 
     * @param ids 需要删除的实时汇报主键
     * @return 结果
     */
    @Override
    public int deleteRealReportByIds(Long[] ids)
    {
        return realReportMapper.deleteRealReportByIds(ids);
    }

    /**
     * 删除实时汇报信息
     * 
     * @param id 实时汇报主键
     * @return 结果
     */
    @Override
    public int deleteRealReportById(Long id)
    {
        return realReportMapper.deleteRealReportById(id);
    }
}
